//
//  AppDelegate.m
//  singlePushDemo
//
//  Created by JM on 15/9/24.
//  Copyright (c) 2015年 JM. All rights reserved.
//

#import "AppDelegate.h"
#import "GeTuiSdk.h"
#import "DYPushCenter.h"


@interface AppDelegate ()<DYSMSProtocol>
//个推透穿过来的数据
@property (copy, nonatomic) NSString *payloadId;////根据 payloadId 取回 Payload（payloadMsg）

@property (assign, nonatomic) SdkStatus *sdkStatus;//个推的状态
@end

//已经收到但是消息未读

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //TODO ios8 才有用
    if ([self.window.rootViewController isKindOfClass:[self.viewCtrl class]]){
        self.viewCtrl = self.window.rootViewController;
    }
    
    //添加appdeleage 为接收者在任意地方使用，使用后被添加者将会接受推送消息
    [[DYPushCenter sharedDYPushCenter] addMessagePushReceiver:self
     ];
    
    //向注册中心注册
    [[DYPushCenter sharedDYPushCenter] registerPushCenterWithHandleBlock:^(NSString *clientID, NSError *error) {
        if (error) {
            UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"DYPush register" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alertView show];
        }
    }];
    [DYPushCenter application:application didFinishLaunchingWithOptions:launchOptions];
    return YES;
}

#pragma mark - 桥接DYPushCenter
- (void)applicationDidEnterBackground:(UIApplication *)application {
    [DYPushCenter applicationDidEnterBackground:application];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [DYPushCenter applicationDidBecomeActive:application];
}

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
    [DYPushCenter application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    [DYPushCenter application:application didFailToRegisterForRemoteNotificationsWithError:error];
}

#pragma mark - addMessagePushReceiver之后 接收的消息
- (void)didReceiveMessage:(id)message withtype:(NSString *)aSMSType {
    if ([aSMSType isEqualToString:DYPushCenter_Message]){
        self.viewCtrl.textView.text = message;
        NSLog(@"收到push的消息");
    }
}

@end
